% ML TAU 2013 final project script
% Alt 5:
% claculate abs(V1-V2), use PCA and then run majority vote with 21 different SVMs on the results 
% (with parameters for SVMS obtained by grid_search.m)

function gogo_alt5()

    %
    % Initialization
    %
    close all; clear; clc; tic;

	libsvm_path = './libsvm-3.17/matlab';

	% Add path to the libsvm
	addpath(libsvm_path);

    
    L = 3; H = 120; N = 21;
    
    if ~exist('dataforproject.mat','file')
        error('Data file not found in current directory')
    end;
    
    % Saving memory. Loading vars on a need to load basis only throughout
    load('dataforproject.mat','X1train','X2train','gidtrain','ytrain');
    fprintf('Training Data successfully loaded\n');

    X_DELTA = abs(X1train-X2train);

    [m,n] = size(X_DELTA);
    
    X_NORM = X_DELTA/max(X_DELTA(:));

    [X_PC COEFF SCORE latent ] = basic_PCA(X_NORM', n, 'X1train-X2train');
    
    TX = X_PC(:,L:H);
    
    if ~exist(sprintf('best_%d_results.mat',N),'file')
        error('Data file not found in current directory')
    end;
    
    load(sprintf('best_%d_results.mat',N),'best_results');
    
    accuracy = 0.0;
    
    for k=1:3
        
       % Training set
       X_train = TX((gidtrain~=k),:);
       y_train = ytrain(gidtrain~=k);
       
       % Testing set
       X_test = TX((gidtrain==k),:);
       y_test = ytrain(gidtrain==k);
       
       predictedY = zeros(size(y_test));
       
        for i=1:N
            
            params = best_results(i,:); % [L H C D T Err]
        
            c = params(3); d = params(4); t = params(5); Err = params(6);
                         
            % Create the model
            model = svmtrain(y_train,X_train,sprintf('-t %d -g  1.0 -c %f -d %d -h 0 -q',t,c,d));
            
            % Predict
            predictedY = predictedY + svmpredict(y_test,X_test,model);
            
        end
        
        results = sum(y_test.*predictedY > 0) / size(y_test,1);
        
        accuracy = accuracy+results;
        
    end
    
    accuracy = accuracy/3;
    
    
    fprintf('#Avg Acc: %f\n',accuracy);
    
end

